 //
//  BQShopCell.m
//  BeeQuick
//
//  Created by Vivitickey on 2017/4/28.
//  Copyright © 2017年 test. All rights reserved.
//

#import "BQShopCell.h"
#import "UIColor+HMAddition.h"
#import "BQClickFoodView.h"
#import "BQRightModel.h"
@interface BQShopCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *pm_descLabel;
@property (weak, nonatomic) IBOutlet UILabel *specificsLabel;
@property (weak, nonatomic) IBOutlet UILabel *patner_priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *market_priceLabel;


@property (nonatomic,weak) BQClickFoodView *clickFoodView;

@end

@implementation BQShopCell
- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 实例化clickFoodView
    BQClickFoodView *clickFoodView = [BQClickFoodView clickFoodView];
    
    _clickFoodView = clickFoodView;
    
    // 添加到cell的 contentView上
    [self.contentView addSubview:clickFoodView];
    
    // 约束
    [clickFoodView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.bottom.offset(-5);
        
        make.width.mas_equalTo(84);
        
        make.height.mas_equalTo(27);
        
    }];
    
    // 添加clickFoodView的监听
    [clickFoodView addTarget:self action:@selector(didClickFoodView:) forControlEvents:UIControlEventValueChanged];
    
}

#pragma mark - 监听点击 -
- (void)didClickFoodView:(BQClickFoodView *)foodView {
    
    
    // clickFoodView上显示的数字和 rightModel中的count绑定起来
//    _rightModel.count = foodView.count;
    
    FMDatabase *db = [[FMDatabase alloc] initWithPath:localDBPath];
    [db open];
    

            [db executeUpdate:@"update BeeQuick set count = ? where id = ?",[NSString stringWithFormat:@"%zd",foodView.count],_rightModel.id];

    
    [db close];
    
    if (foodView.clickDecrement) {
        // 点击的减少按钮
        
        // 发出通知
        [[NSNotificationCenter defaultCenter] postNotificationName:kDecrementNotification object:_rightModel];
        
    } else {
        // 点击的增加按钮
        // 获取到 增加按钮的中心点
        CGPoint imgCenter = self.imgView.center;
        
        // 需要把坐标转为window的坐标
        CGPoint coverPoint = [self.imgView convertPoint:imgCenter toView:kWindow];
    
        
        // 发出通知
        [[NSNotificationCenter defaultCenter] postNotificationName:kIncrementNotification object:_rightModel userInfo:@{@"point":[NSValue valueWithCGPoint:coverPoint]}];
        
    }
    
    

}


-(void)setRightModel:(BQRightModel *)rightModel{
    
    _rightModel = rightModel;
    
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:rightModel.img]];
    
    self.nameLabel.text = rightModel.name;
    
    self.pm_descLabel.text = rightModel.pm_desc;
    
    self.specificsLabel.text = rightModel.specifics;
    
    self.patner_priceLabel.text = [NSString stringWithFormat:@"$%@",rightModel.partner_price];
    
    //    self.market_priceLabel.text = model.market_price;
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"$%@",rightModel.market_price] attributes:attribtDic];
    
    self.market_priceLabel.attributedText = attribtStr;
    
    FMDatabase *db = [[FMDatabase alloc] initWithPath:localDBPath];
    [db open];
    

        FMResultSet *set = [db executeQuery:@"select count from BeeQuick where id = ?",rightModel.id];
        
        while ([set next]) {
            _clickFoodView.count = [set intForColumn:@"count"];
        }
    
    [db close];
}

@end
